package com.itdr.protal.controller;


import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.User;
import com.itdr.pojo.vo.UserVO;
import com.itdr.protal.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    ICartService cartService;


    /**
     * 购物车列表（分页）
     *
     * @param httpSession
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public ServerResponse list(HttpSession httpSession,
                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.listAll(u.getId(), pageNum, pageSize);
    }

    /**
     * 购物车添加商品
     * @param httpSession
     * @param pid
     * @param num
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("add")
    public ServerResponse add(HttpSession httpSession, Integer pid, Integer num,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.add(u.getId(), pid, num, pageNum, pageSize);
    }

    /**
     * 更新购物车中某个商品数量
     * @param httpSession
     * @param pid
     * @param num
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("update")
    public ServerResponse update(HttpSession httpSession, Integer pid, Integer num,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.updateOne(u.getId(), pid, num, pageNum, pageSize);
    }

    /**
     * 移除购物中某个商品
     * @param httpSession
     * @param pid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("delete")
    public ServerResponse delete(HttpSession httpSession, Integer pid,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.deleteOne(u.getId(), pid, pageNum, pageSize);
    }

    /**
     * 清空购物车
     * @param httpSession
     * @return
     */
    @GetMapping("deleteall")
    public ServerResponse deleteAll(HttpSession httpSession) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.deleteAll(u.getId());
    }

    /**
     * 购物车选中或取消某个商品
     * @param httpSession
     * @param pid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("checked")
    public ServerResponse checked(HttpSession httpSession,Integer pid,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.checked(u.getId(),pid,pageNum,pageSize);
    }

    /**
     * 选中或取消全选
     * @param httpSession
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("checkedall")
    public ServerResponse checkedAll(HttpSession httpSession,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.checkedAll(u.getId(),pageNum,pageSize);
    }

    /**
     * 查询购物车中商品数量
     * @param httpSession
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("selectchecked")
    public ServerResponse selectChecked(HttpSession httpSession,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.selectChecked(u.getId(),pageNum,pageSize);
    }

    /**
     *
     * @param httpSession
     * @return
     */
    @GetMapping("total")
    public ServerResponse total(HttpSession httpSession) {
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return cartService.total(u.getId());
    }
}

